import React, { useEffect, useState } from 'react';
import routes from '../../routes';
import Nav from '../Nav';
import event from '../../events';
import { useRoutes } from 'react-router-dom';
import '../../assets/less/base.less';
import './index.less';
import { useAppDispatch } from '../../redux/hook';
import { addtoken } from '../../redux/features/login';
export default function App() {
    const [isLogin, setIsLogin] = useState(false);
    const element = useRoutes(routes);
    const dispatch = useAppDispatch();

    useEffect(() => {
        // 初次监听一次
        event.on('login', (value) => {
            setIsLogin(value);
        });
        // 从本地读取token
        const token = localStorage.getItem('token');
        // 如果token存在，传递给redux
        if (token) {
            dispatch(addtoken(token));
        }
    }, []);

    return (
        <>
            {/* 导航栏 */}
            {isLogin ? '' : <Nav />}
            {/* 路由 */}
            {element}
        </>
    );
}
